﻿using HZH_Controls.Controls;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 渐变.admin_Form
{
    public partial class admin_logFrm : Form
    {
        public admin_logFrm()
        {
            InitializeComponent();
        }

        private void admin_logFrm_Load(object sender, EventArgs e)
        {
           
            this.Dock = DockStyle.Fill;
            cmbPermission.SelectedIndex = 0;
        }

        private void ucDataGridView1_Load(object sender, EventArgs e)
        {
            List<DataGridViewColumnEntity> lstCulumns = new List<DataGridViewColumnEntity>();
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "logId", HeadText = "编号", Width = 30, WidthType = SizeType.AutoSize });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "logName", HeadText = "登录名", Width = 30, WidthType = SizeType.AutoSize });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "logIp", HeadText = "登录IP", Width = 40, WidthType = SizeType.AutoSize });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "logPermission", HeadText = "登录权限", Width = 40, WidthType = SizeType.AutoSize });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "logTime", HeadText = "登录时间", Width = 50, WidthType = SizeType.AutoSize, Format = (a) => { return ((DateTime)a).ToString("yyyy-MM-dd HH:mm:ss"); } });
            this.logdata.Columns = lstCulumns;
            this.logdata.IsShowCheckBox = true;
            string sql = "select * from logTable";
            DataTable dt = new DataTable();
            dt = DBHelper.ExecuteQuery(sql);
            this.logdata.DataSource = dt;
            this.logdata.First();
        }


        public void loadSearch()
        {
            string sql = "select * from logTable where logPermission=@logPermission";
            SqlParameter type = new SqlParameter("@logPermission", cmbPermission.Text);
            if (cmbPermission.Text == "全部信息")
            {
                ucDataGridView1_Load(null, null);
            }
            else
            {
                DataTable dt = new DataTable();
                dt = DBHelper.ExecuteQuery(sql, type);
                //cardata.AutoGenerateColumns = false;
                logdata.DataSource = dt;
            }


        }

        private void cmbPermission_TextChanged(object sender, EventArgs e)
        {
            loadSearch();
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (logdata.SelectRows != null && logdata.SelectRows.Count > 0)
            {
                var uids = new List<int>();
                foreach (var item in logdata.SelectRows)
                {
                    var tempid = Convert.ToInt32((item.DataSource as DataRow)["logId"]);
                    uids.Add(tempid);
                }
                DialogResult dir = MessageBox.Show("您确定要删除该日志吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                if (dir == DialogResult.OK)
                {
                    var uidsstr = new StringBuilder();
                    uidsstr.Append("(");
                    foreach (var item in uids)
                    {
                        uidsstr.Append($"{item},");
                    }
                    uidsstr.Remove(uidsstr.Length - 1, 1);
                    uidsstr.Append(")");
                    string sql = $"delete from logTable where logId IN {uidsstr};";
                    int result = DBHelper.ExecuteNonQuery(sql);
                    if (result > 0)
                    {
                        this.ShowSuccessTip("删除成功！");
                        ucDataGridView1_Load(null, null);
                    }
                    else
                    {
                        this.ShowErrorTip("删除失败！");
                    }
                }
                else
                {
                    return;
                }
            }
            else
            {
                this.ShowErrorTip("未选择任何数据，请选择要删除的数据再进行删除!");
            }
        }
    }
}
